package com.common.utils;

import java.security.MessageDigest;

/**
 * 字符工具类
 */
public class StringUtil {
     //判断字符串是否为空
             public static boolean isEmpty(String str) {
          return str == null || str.trim().length() == 0;
     }
     //判断字符串是否不为空
             public static boolean isNotEmpty(String str) {
          return str != null && str.trim().length() > 0;
     }
     //对字符串进行MD5加密
             public static String createMD5(String passwordSource) {
          String Password;
          byte[] PasswordByte;
          Password = passwordSource;
          try {
              MessageDigest md = MessageDigest.getInstance("MD5");
              md.update(Password.getBytes());
              PasswordByte = md.digest();
          } catch (Exception e) {
              return passwordSource;
          }
          String ReturnPassword = toHex(PasswordByte);
          return ReturnPassword;
     }
     
             //转成哈希
             private static final String toHex(byte[] hash) {
          StringBuilder buf = new StringBuilder(hash.length * 2);
          int i;
          for (i = 0; i < hash.length; i++) {
              if (((int) hash[i] & 0xff) < 0x10) {
                   buf.append("0");
              }
              buf.append(Long.toString((int) hash[i] & 0xff, 16));
          }
          String ret = buf.toString();
          buf = null;
          return ret;
     }
}